/*
 * This file is part of the PathWalker software
 *
 * Copyright (C) 2014 Denis Kvita <dkvita@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <QPainter>
#include <QStyleOption>
#include <QFont>
#include <QColor>
#include <QComboBox>
#include <QAction>

#include <QtDebug>

#include "styleproxymenu.h"

void StyleProxyMenu::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    // paint funny menu item
    if(element == CE_MenuItem){
        if (const QStyleOptionMenuItem *mi= qstyleoption_cast<const QStyleOptionMenuItem *>(option)) {
            QStyleOptionMenuItem *opt= new QStyleOptionMenuItem(*mi);
            QRect rect = option->rect;
            int tagW= 18;
            QRegExp rx("^&(.);.*");
            bool hasIcon = false;
            bool hasCheck = false;
            bool hasTag = false;
            foreach(QAction *act, widget->actions()){
                if(!hasIcon && !act->icon().isNull()) hasIcon= true;
                if(!hasCheck && act->isCheckable()) hasCheck= true;
                if(!hasTag && rx.exactMatch(act->text())) hasTag= true;
            }

            if(opt->menuItemType != QStyleOptionMenuItem::Normal
               && opt->menuItemType != QStyleOptionMenuItem::DefaultItem
               && opt->menuItemType != QStyleOptionMenuItem::SubMenu){
                if(hasIcon || hasCheck){
                    rect.setLeft(tagW+rect.left());
                    opt->rect= rect;
                }
                QProxyStyle::drawControl(element,opt,painter,widget);
                return;
            }

            QString tag= rx.exactMatch(mi->text)?rx.cap(1):"";
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            int tagSize= proxy()->pixelMetric(PM_SmallIconSize, option, widget);
            if(tag.length()>0){
                QFont fnt= opt->font;
                fnt.setBold(true);
                fnt.setPointSizeF(fnt.pointSizeF()*0.9);
                painter->setFont(fnt);
                painter->drawText(mi->rect.x()+1,mi->rect.y()+3,tagSize,tagSize,Qt::AlignCenter, tag);
            }
            painter->restore();

            if(hasIcon || hasCheck) rect.setLeft(tagW+rect.left());
            opt->rect= rect;
            opt->text= QString(mi->text).replace(QRegExp("^&.;"),"");//.left(mi->text.indexOf('\t'))

            QProxyStyle::drawControl(element,opt,painter,widget);
        }
        return;

    }
    QProxyStyle::drawControl(element,option,painter,widget);
}
